package meijuli.com.join51.business.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import org.jetbrains.anko.AsyncKt;

import java.util.ArrayList;

import kotlin.Unit;
import kotlin.jvm.functions.Function1;

/**
 * 网络状态发送器
 *
 * @author Wizong 2017-05-19 13:09
 */
public class NetworkReceiver extends BroadcastReceiver {
    private static ArrayList<NetworkMonitor.OnNetworkListener> mListeners;

    public NetworkReceiver(ArrayList<NetworkMonitor.OnNetworkListener> listeners) {
        mListeners = listeners;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        AsyncKt.runOnUiThread(context, new Function1<Context, Unit>() {
            @Override
            public Unit invoke(Context context) {
                if (NetworkUtil.isConnected()) {
                    for (NetworkMonitor.OnNetworkListener mListener : mListeners) {
                        mListener.onNetworkConnected(NetworkUtil.networkType());
                    }
                } else {
                    for (NetworkMonitor.OnNetworkListener mListener : mListeners) {
                        mListener.onNetworkDisconnected();
                    }
                }
                return null;
            }
        });
    }
}
